package com.tusuo.stds.ui.bindthird;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.FadeEnter.FadeEnter;
import com.flyco.animation.FadeExit.FadeExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.tusuo.stds.Constants;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.UISwitchButton;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_bind_third)
public class BindThirdActivity extends BaseToolbarActivity<BindThirdView, BindThirdPresenter>
                                implements BindThirdView {

    @ViewById(R.id.bind_third_layout_wechat)
    LinearLayout layoutWechat;
    @ViewById(R.id.bind_third_switch_wechat)
    UISwitchButton switchWechat;
    @ViewById(R.id.bind_third_switch_qq)
    UISwitchButton switchQQ;
    @ViewById(R.id.bind_third_switch_weibo)
    UISwitchButton switchWeibo;

    @StringRes(R.string.toolbar_title_bind_third)
    String toolbarTitle;

    @StringRes(R.string.bind_third_tip)
    String strBindThirdTip;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    private UserInfo mUserInfo;

    private boolean mWechatRollback = false;
    private boolean mQQRollback = false;
    private boolean mWeiboRollback = false;

    private NormalDialog mDialog;

    @AfterInject
    void init() {
        mUserInfo = Parcels.unwrap(getIntent().getParcelableExtra(UserInfo.EXTRA_NAME));
    }

    @AfterViews
    void afterViews() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

        boolean bindWechat = false;
        boolean bindQQ = false;
        boolean bindWeibo = false;

        if (mUserInfo != null) {
            bindWechat = mUserInfo.IsWXBind;
            bindQQ = mUserInfo.IsQQBind;
            bindWeibo = mUserInfo.IsWeiboBind;
        }

        switchWechat.setEnabled(false);
        switchQQ.setEnabled(false);
        switchWeibo.setEnabled(false);

        switchWechat.setChecked(bindWechat);
        switchQQ.setChecked(bindQQ);
        switchWeibo.setChecked(bindWeibo);

        switchWechat.setEnabled(true);
        switchQQ.setEnabled(true);
        switchWeibo.setEnabled(true);

        if (!Utils.App.isWXAppInstalled(this)) {
            layoutWechat.setVisibility(View.GONE);
        }

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public BindThirdPresenter createPresenter() {
        return new BindThirdPresenter(this);
    }

    @Override
    protected void onDestroy() {
        if (mDialog != null) {
            mDialog.superDismiss();
            mDialog = null;
        }
        super.onDestroy();
    }

    @CheckedChange(R.id.bind_third_switch_wechat)
    void checkedChangeOnWechatCheckBox(CompoundButton buttonView, boolean isChecked) {
        if (switchWechat.isEnabled()) {
            if (!mWechatRollback) {
                if (isChecked) {
                    Utils.UI.showToastLong(strBindThirdTip);
                    presenter.bindWechat();
                } else {
                    showDialog(Constants.PLATFORM.WECHAT);
                }
            } else {
                mWechatRollback = false;
            }
        }
    }

    @CheckedChange(R.id.bind_third_switch_qq)
    void checkedChangeOnQQCheckBox(CompoundButton buttonView, boolean isChecked) {
        if (switchQQ.isEnabled()) {
            if (!mQQRollback) {
                if (isChecked) {
                    Utils.UI.showToastLong(strBindThirdTip);
                    presenter.bindQQ();
                } else {
                    showDialog(Constants.PLATFORM.QQ);
                }
            } else {
                mQQRollback = false;
            }
        }
    }

    @CheckedChange(R.id.bind_third_switch_weibo)
    void checkedChangeOnWeiboCheckBox(CompoundButton buttonView, boolean isChecked) {
        if (switchWeibo.isEnabled()) {
            if (!mWeiboRollback) {
                if (isChecked) {
                    Utils.UI.showToastLong(strBindThirdTip);
                    presenter.bindWeibo();
                } else {
                    showDialog(Constants.PLATFORM.WEIBO);
                }
            } else {
                mWeiboRollback = false;
            }
        }
    }

    private void showDialog(final String platformCode) {
        BaseAnimatorSet bas_in = new FadeEnter();
        BaseAnimatorSet bas_out = new FadeExit();

        String title = "";

        switch (platformCode) {
            case Constants.PLATFORM.WECHAT:
                title = "微信";
                break;
            case Constants.PLATFORM.QQ:
                title = "QQ";
                break;
            case Constants.PLATFORM.WEIBO:
                title = "微博";
                break;
            default:
                break;
        }

        if (mDialog != null) {
            mDialog.superDismiss();
            mDialog = null;
        }
        mDialog = new NormalDialog(this);
        mDialog.isTitleShow(false)
                .bgColor(Color.parseColor("#FFFFFF"))
                .cornerRadius(5)
                .content(getString(R.string.bind_third_dialog, title))
                .contentGravity(Gravity.CENTER)
                .contentTextColor(Color.parseColor("#333333"))
                .dividerColor(Color.parseColor("#CCCCCC"))
                .btnText("点错了", "确认")
                .btnTextSize(15.5f, 15.5f)
                .btnTextColor(Color.parseColor("#0092FF"), Color.parseColor("#333333"))
                .widthScale(0.7f)
                .showAnim(bas_in)
                .dismissAnim(bas_out);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        mDialog.dismiss();
                        switch (platformCode) {
                            case Constants.PLATFORM.WECHAT:
                                rollbackWechatBindStatus();
                                break;
                            case Constants.PLATFORM.QQ:
                                rollbackQQBindStatus();
                                break;
                            case Constants.PLATFORM.WEIBO:
                                rollbackWeiboBindStatus();
                                break;
                            default:
                                break;
                        }
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        mDialog.dismiss();
                        switch (platformCode) {
                            case Constants.PLATFORM.WECHAT:
                                presenter.unbindWechat();
                                break;
                            case Constants.PLATFORM.QQ:
                                presenter.unbindQQ();
                                break;
                            case Constants.PLATFORM.WEIBO:
                                presenter.unbindWeibo();
                                break;
                            default:
                                break;
                        }
                    }
                }
        );

        mDialog.show();
    }

    @Override
    public void rollbackWechatBindStatus() {
        mWechatRollback = true;
        switchWechat.setChecked(!switchWechat.isChecked());
    }

    @Override
    public void rollbackQQBindStatus() {
        mQQRollback = true;
        switchQQ.setChecked(!switchQQ.isChecked());
    }

    @Override
    public void rollbackWeiboBindStatus() {
        mWeiboRollback = true;
        switchWeibo.setChecked(!switchWeibo.isChecked());
    }
}
